import axios from 'axios'
import sign from './sign'

//域名地址
export let domain = 'https://5fa6525c3625f.woyaorenqi.com'


// 请求接口地址
// let host = 'https://api.woyaorenqi.com/api/v1' //正式接口地址  
let host = domain + '/api/v1' //正式接口地址

// if(window.location.href.indexOf('localhost') != -1) {
//     host = 'http://192.168.8.88:8081/api/v1' // 测试接口地址
//     // host = 'https://5fa6525c3625f.woyaorenqi.com/api/v1'//测试基地址
// }

if (window.location.href.indexOf('192.168.8.88:8082') != -1) {
    host = domain + '/api/v1' //正式接口地址
}

console.log('当前接口地址', host)



const request = async (url, method = 'get', params, tokenpatch) => {
    let token = localStorage.getItem('token')
    let response;
    url = host + url;
    sign.signOfParam(params)
    if (method == 'get') {
        response = await axios.get(url, {
            headers: {
                'Authorization': 'Bearer ' + token
            },
            params: params
        })
    } else {
        let token = localStorage.getItem('token')
        if (token) {
            response = await axios({
                url: url,
                method: 'post',
                data: params,
                headers: {
                    'Authorization': 'Bearer ' + token,
                    // 'Contennt-Type': 'application/x-www-from-urlencoded'
                }
            })
        } else {
            response = await axios({
                url: url,
                method: 'post',
                data: params,
            })
        }
    }
    return response
}

export default {
    request
}
